/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmBusquedaPartidoPolitico.java
 *
 * Created on 20/05/2011, 11:59:59 AM
 */

package verins.GUI;

import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.table.DefaultTableModel;
import verins.BO.PartidopoliticoBO;
import verins.BO.TipoPartidoPoliticoBO;
import verins.beans.Partidopolitico;
import verins.beans.Tipopartidopolitico;
import verins.beans.Usuario;




/**
 *
 * @author yisse
 */
public class frmBusquedaPartidoPolitico extends JDialog {

     List<Partidopolitico> lstPartidos = null;
     public Partidopolitico partidoPolitico =null;

    /** Creates new form frmBusquedaPartidoPolitico */
    public frmBusquedaPartidoPolitico(Usuario usuario, JDialog vent) {
        super(vent, true);
        initComponents();
        lblCoincidencias.setVisible(true);
        verins.Utils.Comunes.centrarVentana(this);
        getRootPane().setDefaultButton(btnBuscar);
        txtPartidoPolitico.requestFocus();
        setearValidadores();
        llenarCombos();
              
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        lblPartidoPolitico = new javax.swing.JLabel();
        txtPartidoPolitico = new javax.swing.JTextField();
        lblTipo = new javax.swing.JLabel();
        lblEstado = new javax.swing.JLabel();
        cmbTipo = new javax.swing.JComboBox();
        cmbEstado = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        btnBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblPartidoPolitico = new javax.swing.JTable();
        btnNuevo = new javax.swing.JButton();
        btnVerDetalle = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        lblCoincidencias = new javax.swing.JLabel();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Búsqueda de partidos políticos");
        setResizable(false);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterios de Búsqueda"));

        lblPartidoPolitico.setText("Partido Político");

        lblTipo.setText("Tipo");

        lblEstado.setText("Estado");

        cmbEstado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar", "Activo", "Inactivo" }));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblPartidoPolitico)
                    .addComponent(lblTipo))
                .addGap(40, 40, 40)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                        .addComponent(lblEstado)
                        .addGap(18, 18, 18)
                        .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(34, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(txtPartidoPolitico, javax.swing.GroupLayout.PREFERRED_SIZE, 478, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPartidoPolitico)
                    .addComponent(txtPartidoPolitico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTipo)
                    .addComponent(cmbTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblEstado))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        jLabel1.setText("PROCESO ELECTORAL");

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        tblPartidoPolitico.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Nombre del Partido", "Tipo de Partido", "Fecha de Registro", "Estado"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblPartidoPolitico);
        tblPartidoPolitico.getColumnModel().getColumn(0).setResizable(false);
        tblPartidoPolitico.getColumnModel().getColumn(1).setResizable(false);
        tblPartidoPolitico.getColumnModel().getColumn(2).setResizable(false);
        tblPartidoPolitico.getColumnModel().getColumn(3).setResizable(false);

        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        btnVerDetalle.setText("Ver detalle");
        btnVerDetalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVerDetalleActionPerformed(evt);
            }
        });

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        lblCoincidencias.setText("Se encontraron (0) coincidencias");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(179, 179, 179)
                            .addComponent(btnVerDetalle)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jScrollPane1)
                        .addComponent(jLabel1)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lblCoincidencias)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 325, Short.MAX_VALUE)
                        .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBuscar)
                    .addComponent(lblCoincidencias))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnNuevo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnVerDetalle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSalir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        // TODO add your handling code here:
            partidoPolitico=null;
            this.dispose();
 
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        // TODO add your handling code here:
        if (((JButton)evt.getSource()).getText().equalsIgnoreCase("nuevo"))
            new frmPartidoPolitico(null,null).setVisible(true);
        else{
            //agregar objeto en la otra ventana
            if (obtenerPartidoSeleccionado()!=null)
                dispose();
            else{
                verins.Utils.Mensajes.alerta("Debe seleccionar un partido");
            }
        }
    }//GEN-LAST:event_btnNuevoActionPerformed

    public void setearBotonesBusqueda(){
        btnNuevo.setText("Seleccionar");
        btnVerDetalle.setVisible(false);
    }
    
    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed

         partidoPolitico= obtenerCriterios();
         llenarTabla();
         if (lstPartidos==null || lstPartidos.isEmpty()){
             lblCoincidencias.setVisible(true);
         }else{
             lblCoincidencias.setVisible(false);
         }
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnVerDetalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVerDetalleActionPerformed
        // TODO add your handling code here:
           if (lstPartidos!=null){
            int fila=tblPartidoPolitico.getSelectedRow();
            if (fila>=0){
                frmPartidoPolitico frm=new  frmPartidoPolitico(lstPartidos.get(fila),this);
                frm.bloquearCamposMostrar();
                frm.setearBotonGuardarEditar("Editar");
                frm.setearBotonSalirCancelar("Salir");
                frm.setVisible(true);
            }
        }
    }//GEN-LAST:event_btnVerDetalleActionPerformed

    private void llenarCombos(){
        List lista=TipoPartidoPoliticoBO.findAll();
        verins.Utils.Comunes.llenarCombo(cmbTipo,lista);
        cmbTipo.insertItemAt((Object)new Tipopartidopolitico(0,"Todos"),0);
        cmbTipo.setSelectedIndex(0);
    }

    

     public void llenarTabla(){
        SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
        verins.Utils.Comunes.limpiarTabla(tblPartidoPolitico);
        lstPartidos = PartidopoliticoBO.Buscarpartido(partidoPolitico);

        DefaultTableModel dtm=(DefaultTableModel)tblPartidoPolitico.getModel();
        if (lstPartidos!=null&& lstPartidos.size()>0){
            Iterator it=lstPartidos.iterator();
            while(it.hasNext()){
                Partidopolitico p=(Partidopolitico)it.next();
                Object fila[]=new Object[] {p.getNombre(),p.getIdtipopartidopolitico(),sdf.format(p.getFecharegistro()),p.getEstado()};
                dtm.addRow(fila);
            }
            verins.Utils.Comunes.setearColumnas(tblPartidoPolitico);
        }
    }

     public Partidopolitico obtenerPartidoSeleccionado(){
         
           if (lstPartidos!=null){
            int fila=tblPartidoPolitico.getSelectedRow();
                if (fila>=0){
                    partidoPolitico=lstPartidos.get(fila);
                } else 
                    partidoPolitico= null;
           }
           return partidoPolitico;
     }


     private void setearValidadores(){
         txtPartidoPolitico.setDocument(new verins.Utils.LimitInput(45));
         txtPartidoPolitico.addKeyListener(verins.Utils.Verificador.verificaSoloCaracteresValidos());

    }


    private Partidopolitico obtenerCriterios()
    {

        Partidopolitico partido=new Partidopolitico();

        partido.setNombre(txtPartidoPolitico.getText());
        partido.setIdtipopartidopolitico((Tipopartidopolitico)cmbTipo.getSelectedItem());
        partido.setEstado(cmbEstado.getSelectedItem().toString());

     return partido;
    }

    public void seteaTipoPartido(Tipopartidopolitico tipo){
        cmbTipo.setSelectedItem(tipo);
        cmbTipo.setEnabled(false);
    }
     public void seteaPartidoActivo(){
        cmbEstado.setSelectedItem("Activo");
        cmbEstado.setEnabled(false);
    }

    /**
    * @param args the command line arguments
    */


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnVerDetalle;
    private javax.swing.JComboBox cmbEstado;
    private javax.swing.JComboBox cmbTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCoincidencias;
    private javax.swing.JLabel lblEstado;
    private javax.swing.JLabel lblPartidoPolitico;
    private javax.swing.JLabel lblTipo;
    private javax.swing.JTable tblPartidoPolitico;
    private javax.swing.JTextField txtPartidoPolitico;
    // End of variables declaration//GEN-END:variables

}
